package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.woniu.controller.form.AddHandelForm;
import com.woniu.model.FlowHandle;
import com.woniu.model.Result;
import com.woniu.service.FlowHandleService;
import com.woniu.service.param.AddHandelParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  流程执行表执行类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@Api(tags = "流程相关接口")
@RequestMapping("/flowHandle")
public class FlowHandleController {
    @Resource
    private FlowHandleService flowHandleService;

    /**
     * 查询一个订单当前走到哪一步了
     * @param orderNum 订单号
     */
    @GetMapping("/findNow/{orderNum}")
    @ApiOperation("查询一个订单当前走到哪一步了")
    public Result<FlowHandle> locationConfirm(@PathVariable("orderNum") String orderNum) {
        FlowHandle flowHandle = flowHandleService.findNow(orderNum);
        return Result.newInstance(FlowHandle.class).setData(flowHandle).setMessage("站点确认成功!").setCode(200);
    }


    /**
     * 创建流程执行
     * @param form 添加流程执行form
     */
    @PostMapping("/add")
    @ApiOperation("创建流程执行")
    public Result<FlowHandle> add(@RequestBody AddHandelForm form){
        FlowHandle flowHandle = null;
        try {
            flowHandle = flowHandleService.add(BeanUtil.toBean(form, AddHandelParam.class));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        System.out.println(JSONUtil.parseObj(flowHandle));
        return Result.newInstance(FlowHandle.class).setData(flowHandle).setMessage("创建成功!").setCode(200);
    }

}

